package com.zhb.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping
public class IndexController {
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 
	 * @Title: dc
	 * @author: zhb
	 * @date: 2017年7月11日 下午1:56:17
	 * @Description: 说明@GetMapping注解继承了RequestMapping ,只针对get请求
	 * @return
	 */
	@GetMapping("/consumer")
	public String dc() {
		/**
		 * 通过服务的Id:zhb-cloud-eureka-client查找当前的服务,访问当前服务的应用
		 */
		ServiceInstance serviceInstance = loadBalancerClient.choose("zhb-cloud-eureka-client");
		String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
		System.out.println(url);
		return restTemplate.getForObject(url, String.class);
	}
}